/*
 * @Author: wangdazhuang
 * @Date: 2024-08-26 16:25:58
 * @LastEditTime: 2025-02-18 15:36:35
 * @LastEditors: wangdazhuang
 * @Description: 
 * @FilePath: /pornhub_app/lib/views/main/views/main_page.dart
 */
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:lazy_load_indexed_stack/lazy_load_indexed_stack.dart';
import 'package:pornhub_app/components/image_view.dart';
import 'package:pornhub_app/components/navigation_bar/easy_bottom_navigation_bar.dart';
import 'package:pornhub_app/generate/app_image_path.dart';
import 'package:pornhub_app/services/app_service.dart';
import 'package:pornhub_app/utils/ad_jump.dart';
import 'package:pornhub_app/utils/enum.dart';
import 'package:pornhub_app/utils/extension.dart';
import 'package:pornhub_app/utils/initAdvertisementInfo.dart';
import 'package:pornhub_app/views/community/community_page.dart';
import 'package:pornhub_app/views/main/controllers/main_controller.dart';
import 'package:pornhub_app/views/main/views/tabs/shi_pin_tab.dart';
import 'package:pornhub_app/views/mine/mine_page.dart';
import 'package:pornhub_app/views/home/home_page.dart';

import '../../forbidden/forbidden_page.dart';

class MainPage extends StatelessWidget {
  const MainPage({super.key});

  BottomNavigationBarItem _buildBarItem(
      {required String label,
      required String icon,
      required String activeIcon}) {
    return BottomNavigationBarItem(
      label: label,
      icon: Image.asset(icon, width: 26.w, height: 26.w),
      activeIcon: Image.asset(activeIcon, width: 26.w, height: 26.w),
    );
  }

  Widget initTabbar() {
    final homeContoller = Get.find<MainController>();
    return EasyBottomNavigationBar.common(
      currentIndex: homeContoller.currentIndex.value,
      onTap: homeContoller.changeMainTabIndex,
      items: [
        _buildBarItem(
          label: '首页',
          icon: AppImagePath.tabbar_t1,
          activeIcon: AppImagePath.tabbar_t1_y,
        ),
        _buildBarItem(
          label: '视频',
          icon: AppImagePath.tabbar_t2,
          activeIcon: AppImagePath.tabbar_t2_y,
        ),
        if (Get.find<AppService>().hasForbidden)
          _buildBarItem(
            label: '禁区',
            icon: AppImagePath.tabbar_t3,
            activeIcon: AppImagePath.tabbar_t3_y,
          ),
        _buildBarItem(
          label: '社区',
          icon: AppImagePath.tabbar_t4,
          activeIcon: AppImagePath.tabbar_t4_y,
        ),
        _buildBarItem(
          label: '我的',
          icon: AppImagePath.tabbar_t5,
          activeIcon: AppImagePath.tabbar_t5_y,
        ),
      ],
    );
  }

  Widget buildAd() {
    final rightAd =
        initWeightAdvertisementInfo(AdPlaceNameEnum.CORNER_LEVITATE);
    if (rightAd == null) return const SizedBox.shrink();
    ValueNotifier<bool> showRightAd = ValueNotifier(true);
    return ValueListenableBuilder(
        valueListenable: showRightAd,
        builder: (context, value, child) {
          return value
              ? Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Icon(
                      Icons.cancel,
                      color: Colors.white,
                      size: 30.w,
                    ).onTap(() {
                      showRightAd.value = false;
                    }),
                    SizedBox(
                      height: 5.w,
                    ),
                    SizedBox(
                      width: 80.w,
                      height: 80.w,
                      child: ImageView(src: rightAd.adImage ?? ''),
                    ).onTap(() {
                      kAdjump(rightAd.adJump, rightAd.adId);
                    }),
                  ],
                ).paddingBottom(60.w)
              : const SizedBox.shrink();
        });
  }

  @override
  Widget build(BuildContext context) {
    final homeContoller = Get.find<MainController>();
    return SafeArea(
      top: false,
      bottom: kIsWeb ? true : false,
      child: Scaffold(
        floatingActionButton: buildAd(),
        floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
        body: Obx(() => LazyLoadIndexedStack(
              index: homeContoller.currentIndex.value,
              preloadIndexes: const [0],
              children: [
                const HomePage(),
                const ShiPinTab(),
                if (Get.find<AppService>().hasForbidden) const ForbiddenPage(),
                const CommunityPage(),
                const MinePage(),
              ],
            )),
        bottomNavigationBar: Obx(
          () => kIsWeb
              ? Container(
                  color: Colors.black,
                  padding: const EdgeInsets.only(bottom: 20),
                  child: initTabbar())
              : initTabbar(),
        ),
      ),
    );
  }
}
